package Sequence::Infinite::Prime;
use base 'Sequence::Infinite::Natural';
use strict;

my $first_prime = new Sequence::Infinite::Natural;
$first_prime->increment; $first_prime->increment; 
our @primes = ( $first_prime->increment );

sub increment {
    my( $this ) = @_;
    my $int = $this->SUPER::increment;
    while( not is_prime($int) ) {
        $int = $this->SUPER::increment;
    }
    $int
}

sub is_prime {
    my( $int ) = @_;
    return 1 if grep { $_ == $int } @primes;
    return undef if $int < $primes[0];
    
    my $max = int( sqrt($int)+1 );
    for my $i ( @primes ) {
        next if $i > $max;
        return undef unless $int % $i;
    }
    
    push( @primes, $int ) if $primes[ @primes - 1 ] < $int;
    
    1;
}

1;
